package com.qiang.demo01;

import com.qiang.demo01.domain.Student;

import java.util.Comparator;
import java.util.TreeSet;

public class Test02 {
    public static void main(String[] args) {
        /*TreeSet<Student> ts = new TreeSet<>();
        Student st = new Student("张三", 25);
        Student st1 = new Student("李四", 25);
        Student st2 = new Student("王麻子", 24);
        Student st3 = new Student("赵六", 22);
        ts.add(st);
        ts.add(st1);
        ts.add(st2);
        ts.add(st3);
        System.out.println(ts);*/

        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {

            @Override
            public int compare(Student o1, Student o2) {
                //o1表示现在要存入的那个元素
//o2表示已经存入到集合中的元素
//主要条件
                int result = o1.getAge() - o2.getAge();
//次要条件
                result = result == 0 ? o1.getName().compareTo (o2.getName()) : result;
                return result;
            }
        });
        Student st = new Student("张三", 25);
        Student st1 = new Student("李四", 25);
        Student st2 = new Student("王麻子", 24);
        Student st3 = new Student("赵六", 22);
        ts.add(st);
        ts.add(st1);
        ts.add(st2);
        ts.add(st3);
        for (Student Student : ts) {
            System.out.println(Student);
        }

    }
}
